package pl.smath.renderer.model.sign;

import com.google.gwt.user.client.ui.RootPanel;
import gwt.g2d.client.graphics.Surface;
import pl.smath.expression.model.presentation.ExtendedRelationType;
import pl.smath.renderer.utils.Cursor;

public class RelationSign extends AbstractSign {

	protected String text;
	
	public RelationSign() {
		super();
		text = "=";
	}
	public RelationSign(ExtendedRelationType type) {
		super();
		text = type2string(type);
	}

	public RelationSign(String type) {
		super();
		text = type2string(ExtendedRelationType.string2type(type));
	}

	private String type2string(ExtendedRelationType type){
		switch (type){
			case EQUAL: return "=";
			case NOT_EQUAL: return String.valueOf((char)8800);
			case GREATER_THAN: return ">";
			case GREATER_THAN_OR_EQUAL_TO: return String.valueOf((char)8805);
			case LESS_THAN: return "<";
			case LESS_THAN_OR_EQUAL_TO: return String.valueOf((char)8804);
			case EQUVALENT: return "~";
			case IDENTICAL: return String.valueOf((char)8801);
			case APPROXIMATELY_EQUAL: return String.valueOf((char)8776);
			case ISOMORPHIC: return String.valueOf((char)8773);
			case ORTHOGONAL: return String.valueOf((char)8869);
			case PARALLEL: return "||";
		}
		return "=";
	}
	
	@Override
	public void render(Surface canvas, Cursor cursor) {
		canvas.fillText(text, cursor.getX() + this.getSpace(), cursor.getY());
		/*
		Double x = cursor.getX() + this.getSpace();
		Double y0 = cursor.getY() - this.getFontHeight() * 0.25d;
		Double y1 = cursor.getY() - this.getFontHeight() * 0.45d;
		Double length = this.getSignWidth();
		Double thickness = this.getBrushSize();
		
		canvas.fillRectangle(x, y0, length, thickness);
		canvas.fillRectangle(x, y1, length, thickness);*/
	}

	@Override
	public void update() {		
		//this.setSignWidth(this.getFontHeight() * 0.6d);
		//this.setWidth(this.getSignWidth() + 2d * this.getSpace());
		
		Surface surface = new Surface();
		RootPanel.get().add(surface);
		surface.setFont(this.getFont());
		this.setSignWidth(surface.measureText(text));
		this.setWidth(this.getSignWidth() + 2d * this.getSpace());
		this.setHeight(this.getFontHeight());
		RootPanel.get().remove(surface);
	}

}
